package org.coco.cloud.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.coco.cloud.order.bean.Order;
import org.coco.cloud.order.feign.ProductFeignClient;
import org.coco.cloud.order.service.OrderService;
import org.coco.cloud.product.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @Author fengleifl-hzgs
 * @Date 2025/1/19
 * @since
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired // 一定导入 spring-cloud-starter-loadbalancer
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder")
    @Override
    public Order createOrder(Long productId, Long userId) {

//        Product product = getRemoteProductLoadBalanceAnnotation(productId);
        Product product = productFeignClient.getProductById(productId);

        System.out.println(productFeignClient.getClass());

        Order order = new Order();
        order.setId(userId * 10);
        order.setUserId(userId);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setAddress("北京");
        order.setNickName("coco");
        order.setProductList(Arrays.asList(product));

        return order;
    }

    public Product getRemoteProduct(Long productId) {
        // 1、获取远程商品服务所在得所有机器IP+Port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        // 远程url http://localhost:9001/product/4
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;

        log.info("url:{}", url);
        // 2、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }

    public Product getRemoteProductLoadBalance(Long productId) {
        // 1、获取远程商品服务所在得所有机器IP+Port
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-product");
        // 远程url http://localhost:9001/product/4
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;

        log.info("url:{}", url);
        // 2、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }

    public Product getRemoteProductLoadBalanceAnnotation(Long productId) {
        // 远程url http://localhost:9001/product/4
        String url = "http://service-product/product/" + productId;
        log.info("url:{}", url);
        // 2、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }

}
